package com.azkj.album.view.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.azkj.album.R;
import com.azkj.album.utils.DensityUtils;


public class CommonDialog {


    public static final class Builder {

        private Context mContext;


        private TextView mTvTitle;
        private TextView mTvDesc;
        private TextView mTvConfirm;
        private TextView mTvCancel;
        private LinearLayout mLLContent;


        private OnClickListener mListener;


        private final Dialog dialog;

        public Builder(Context context) {
            this.mContext = context;
            View view = LayoutInflater.from(context).inflate(
                    R.layout.dialog_common, null);

            mTvTitle = view.findViewById(R.id.tv_title);
            mTvDesc = view.findViewById(R.id.tv_desc);
            mTvConfirm = view.findViewById(R.id.tv_confirm);
            mTvCancel = view.findViewById(R.id.tv_cancel);
            mLLContent = view.findViewById(R.id.ll_content);


            // 定义Dialog布局和参数
            dialog = new Dialog(context, R.style.MyDialog);
            dialog.setContentView(view);
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.x = 0;
            lp.y = 0;
            lp.width = (int) (DensityUtils.getScreenWidth(context));
            lp.height = (int) (DensityUtils.getScreenHeight(context));
            lp.gravity = Gravity.CENTER;
            dialogWindow.setAttributes(lp);

            mTvConfirm.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onConfirm(dialog, true);
                }
            });

            mTvCancel.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onConfirm(dialog, false);
                }
            });

        }

        public Builder setTitle(String title) {
            this.mTvTitle.setText(title);
            this.mTvTitle.setVisibility(View.VISIBLE);
            return this;
        }


        public Builder setDesc(String title) {
            this.mTvDesc.setText(title);
            this.mTvDesc.setVisibility(View.VISIBLE);
            return this;
        }

        public Builder setDesc(Spanned title) {
            this.mTvDesc.setText(title);
            this.mTvDesc.setVisibility(View.VISIBLE);
            return this;
        }

        public Builder setDescLine(int line) {
            this.mTvDesc.setMaxLines(line);
            this.mTvDesc.setEllipsize(TextUtils.TruncateAt.END);
            this.mTvDesc.setVisibility(View.VISIBLE);
            return this;
        }

        public Builder setSingleBtn(boolean single) {
            this.mTvCancel.setVisibility(single ? View.GONE : View.VISIBLE);
            return this;
        }

        public Builder setBackground(int resId) {
            this.mLLContent.setBackgroundResource(resId);
            return this;
        }

        public Builder setButton(String left, String right) {
            if (!TextUtils.isEmpty(left)) {
                this.mTvCancel.setText(left);
            } else {
                this.mTvCancel.setVisibility(View.GONE);
            }
            if (!TextUtils.isEmpty(right)) {
                this.mTvConfirm.setText(right);
            }
            return this;
        }


        public Builder setOnClickListener(OnClickListener listener) {
            this.mListener = listener;
            return this;
        }


        public Builder show() {
            dialog.show();
            return this;

        }


    }

    public interface OnClickListener {
        void onConfirm(Dialog dialog, boolean confirm);
    }


}
